package com.tongji.seller.view
{
	import com.tongji.seller.events.GetSavedProductEvent;
	import com.tongji.seller.events.GetStoreItemInfoEvent;
	import com.tongji.seller.events.GetStoreItemsEvent;
	import com.tongji.seller.events.GetStoreSavedSceneEvent;
	import com.tongji.seller.events.GetStoreSceneDetailEvent;
	import com.tongji.seller.events.GetStoreSystemSceneHotStateEvent;
	import com.tongji.seller.events.GetStoreUserSceneEvent;
	import com.tongji.seller.events.GetTopHotSceneEvent;
	import com.tongji.seller.events.GetTypesEvent;
	import com.tongji.seller.events.utilsEvent.ChangeStepEvent;
	import com.tongji.seller.model.GetSystemSceneModel;
	import com.tongji.seller.model.StoreProductModel;
	import com.tongji.seller.model.StoreSceneModel;
	import com.tongji.seller.model.UserModel;
	import com.tongji.seller.utils.PopUpAdmin;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	import mx.events.CollectionEvent;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author caddy
	 * 
	 */	
	public class HomeViewMediator extends Mediator
	{
		[Inject]
		public var view:HomeView;

		[Inject]
		public var sceneModel:GetSystemSceneModel;
		
		[Inject]
		public var storeSceneModel:StoreSceneModel;
		
		[Inject]
		public var userModel:UserModel;

		[Inject]
		public var productModel:StoreProductModel;
		
		override public function onRegister():void
		{
			BindingUtils.bindProperty(view.recentHotList, "dataProvider", storeSceneModel, "topHotSceneArray");
			BindingUtils.bindProperty(view.productText, "text", productModel, "savedProductCountStr");
			BindingUtils.bindProperty(view.sceneText, "text", storeSceneModel, "savedSceneCount");

			eventMap.mapListener(view.editSceneBtn, MouseEvent.CLICK, toStepOneHandler, MouseEvent);
			eventMap.mapListener(view.editProductBtn, MouseEvent.CLICK, toStepTwoHandler, MouseEvent);
			eventMap.mapListener(view.editBtn, MouseEvent.CLICK, toStepThirdHandler, MouseEvent);
			eventMap.mapListener(view.createNew, MouseEvent.CLICK, createNewHandler, MouseEvent);
			eventMap.mapListener(view.editMySceneBtn, MouseEvent.CLICK, editMySceneHandler, MouseEvent);
			
			dispatch(new GetTypesEvent(GetTypesEvent.GET_PLACES));
			dispatch(new GetTypesEvent(GetTypesEvent.GET_STYLES));
			dispatch(new GetTopHotSceneEvent(GetTopHotSceneEvent.GET_STORE_HOT_SCENE, 12));

			eventMap.mapListener(view.ok, MouseEvent.CLICK, useridHandler, MouseEvent);

		}
		
		private function useridHandler(event:MouseEvent):void
		{
			userModel.currentUserID = int(view.userID.text);
			dispatch(new GetStoreUserSceneEvent(GetStoreUserSceneEvent.GET_STORE_USER_SCENE, userModel.currentUserID, 1, 15));

			dispatch(new GetStoreItemsEvent(GetStoreItemsEvent.GET_ALL_ITEMS, "亿好旗舰店", "", 1, 6));
			dispatch(new GetSavedProductEvent(GetSavedProductEvent.GET_SAVED_PRODUCT, userModel.currentUserID));
			dispatch(new GetStoreItemInfoEvent(GetStoreItemInfoEvent.GET_STORE_ITEM, userModel.currentUserID, 1, 3));
			dispatch(new GetStoreItemInfoEvent(GetStoreItemInfoEvent.GET_STORE_PRODUCT, userModel.currentUserID, 1, 10));

			dispatch(new GetStoreSystemSceneHotStateEvent(GetStoreSystemSceneHotStateEvent.GET_STORE_SYSTEM_SCENE_HOT_STATE, userModel.currentUserID, "","", 1));
			dispatch(new GetStoreSavedSceneEvent(GetStoreSavedSceneEvent.GET_SAVED_SCENE, userModel.currentUserID));
		}
		
		private function editMySceneHandler(event:MouseEvent):void
		{
			PopUpAdmin.popupCenterWindow(PopUpAdmin.MY_SCENE_TITLE_WIN, view.parent.parent, mediatorMap);
		}
		
		private function createNewHandler(event:MouseEvent):void
		{
			userModel.currentState = 1;
			dispatch(new ChangeStepEvent(ChangeStepEvent.TO_FIRST_STEP));
		}

		private function toStepThirdHandler(event:MouseEvent):void
		{
			if (storeSceneModel.savedSceneCount == "0")
			{
				Alert.show("sorry");
			}
			else
				dispatch(new ChangeStepEvent(ChangeStepEvent.TO_THIRD_STEP));
		}
		
		private function toStepTwoHandler(event:MouseEvent):void
		{
			dispatch(new ChangeStepEvent(ChangeStepEvent.TO_SECOND_STEP));			
		}
		
		private function toStepOneHandler(event:MouseEvent):void
		{
			dispatch(new ChangeStepEvent(ChangeStepEvent.TO_FIRST_STEP));	
		}		
		
	}
}